import {createStore, bindActionCreators, applyMiddleware} from 'redux';
import {createLogger} from 'redux-logger'
import thunk from "redux-thunk";
import reducer from "./reducer";
import {default as createSagaMiddleware}  from 'redux-saga'
import {takeEvery,delay,takeLatest,take,put,fork,cancel} from 'redux-saga/effects'
// import * as action_login from './action/action-login'
import * as action_users from './action/action-users'
import * as action_users_type from './action/action-users-type'

const saga = createSagaMiddleware()
let takes = null
function* callbacks(action) {
    if(takes){
        yield cancel(takes)
    }
    takes = yield fork(function* () {
        yield delay(1000)
        console.log('test')
    })
    yield delay(500)
    console.log(1)
}
function synPromise(){
    return new Promise((resolve, reject) => {
        setTimeout(()=>{
            resolve(300)
        },300)
    })
}
function* putSaga(action){
    yield put(action)
}
function* waction(){
    yield takeEvery(action_users_type.AddUser,callbacks)
}

function getPromiseData() {
    return function (dispatch) {
        let index = 1,timer = null;
        timer = setInterval(() => {
            if(index>=3){
                clearInterval(timer);
                timer = null
            }
            dispatch(action_users.createAddUserAction({
                id: index, name: '清磷', age: 21
            }))
            index++
        },1000)
    }
}
function Middleware1() {
    return function (next) {
        return function (action) {
            console.log('Middleware1')
            next(action)
        }
    }
}

const Middleware2 = () => next => action => {
    console.log('Middleware2')
    next(action)
}


const logger = createLogger({
    duration: true,
    timestamp: true,
    level: 'log',
    // diff: true,
})

let store = createStore(reducer, applyMiddleware(saga))
saga.run(waction)
// let store = applyMiddleware(Middleware1, Middleware2, logger)(createStore)(reducer)

window.dispatch  = ()=>{
    store.dispatch(action_users.createAddUserAction({id:1,name:'清磷'}))
}

store.dispatch(action_users.createAddUserAction({id:1,name:'清磷'}))
store.dispatch(action_users.createAddUserAction({id:1,name:'清磷'}))
store.dispatch(action_users.createAddUserAction({id:1,name:'清磷'}))
store.dispatch(action_users.createAddUserAction({id:1,name:'清磷'}))

